package com.xauto.pioneer.appuser.http;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

import com.xauto.pioneer.appuser.activity.LoginActivity;
import com.xauto.pioneer.appuser.model.JSONStatus;
import com.xauto.pioneer.appuser.util.PreferencesUtils;
import com.xauto.pioneer.appuser.util.StringUtils;
import com.xauto.pioneer.appuser.view.LoadingDialog;

/**
 * Created by Lemon on 2016/1/23.
 * Desc:网路请求二次封装
 */
public class BaseLHttpHandler implements LHttpResponseHandler {

    /** 是否显示加载进度圈*/
    private boolean isShowLoading = false;
    /** 是否显示错误吐司*/
    private boolean isShowFailureToast = true;
    private LoadingDialog loadingDialog;
    private Context mContext;

    public BaseLHttpHandler(Context mContext, boolean isShowLoading, boolean isShowFailureToast) {
        this.isShowLoading = isShowLoading;
        this.isShowFailureToast = isShowFailureToast;
        this.mContext = mContext;
    }

    public BaseLHttpHandler(Context mContext, boolean isShowLoading) {
        this.isShowLoading = isShowLoading;
        this.mContext = mContext;
    }

    public BaseLHttpHandler(Context mContext) {
        this.mContext = mContext;
    }

    @Override
    public void onStarting() {
        Log.d("BaseLHttpHandler","onStarting");
        if(loadingDialog == null) {
            loadingDialog = new LoadingDialog(mContext, "正在加载");
        }
        if(isShowLoading) {
            if(!loadingDialog.isShowing()) {
                loadingDialog.show();
            }
        }
    }

    @Override
    public void onLoading(long count, long current) {

    }

    @Override
    public void onSuccess(JSONStatus jsonStatus) {
        Log.e("BaseLHttpHandler","onSuccess");
        if(isShowLoading) {
            if(loadingDialog.isShowing()) {
                loadingDialog.dismiss();
            }
        }
    }

    @Override
    public void onFailure(Throwable t, int errorNo, String strMsg) {
        Log.e("BaseLHttpHandler","onFailure errorNo:"+errorNo+" strMsg:"+strMsg);
        if (isShowLoading) {
            if (loadingDialog.isShowing()) {
                loadingDialog.dismiss();
            }
        }
        if (!StringUtils.isBlank(strMsg)) {
            if (errorNo == 2002||errorNo == 1011) {//非法TOKEN
                Toast.makeText(mContext, strMsg, Toast.LENGTH_SHORT).show();
                PreferencesUtils.clear(mContext);
                //ActivityStackManager.getActivityManager().finishAll();如果这块删除，那么导致登陆后直接退出
                mContext.startActivity(new Intent(mContext, LoginActivity.class));
            } else {
                if (isShowFailureToast)
                    Toast.makeText(mContext, strMsg, Toast.LENGTH_SHORT).show();
            }
        } else {
            if (isShowFailureToast)
                Toast.makeText(mContext, "服务器出错啦", Toast.LENGTH_SHORT).show();
        }
        if (loadingDialog.isShowing()) {
            loadingDialog.dismiss();
        }
    }

}
